package _8_IO._3_OutputStream;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 *
 * OutputStream
 *
 * 学习出处：https://www.liaoxuefeng.com/wiki/1252599548343744/1298069169635361
 *
 *
 */
public class OutputStreamTest {

    @Test
    public void writeFile() throws IOException {
        String path = "D://";
        String fileName = "outPutStream.txt";
        OutputStream outputStream = new FileOutputStream(path + fileName);
        outputStream.write(72);
        outputStream.write(105);
        outputStream.write(122);
        outputStream.write(47);
        outputStream.write(65);

    }

    @Test
    public void writeFile1() throws IOException {
        String path = "D://";
        String fileName = "outPutStream.txt";
        OutputStream outputStream = new FileOutputStream(path + fileName);
        byte[] bytes = "Hello".getBytes("UTF-8");
        outputStream.write(bytes);
    }

    @Test
    public void writeFile2() throws IOException {
        String path = "D://";
        String fileName = "outputStream.txt";

        try(OutputStream outputStream = new FileOutputStream(path + fileName)){
            outputStream.write("jiangboyang".getBytes(StandardCharsets.UTF_8));
        }// 编译器在此自动为我们写入finally并调用close()
    }

    /**
     * 复制
     * @throws IOException
     */
    @Test
    public void writeFile3()  throws IOException {
        String sourcePath = "D://";
        String sourceileName = "input.txt";

        String targetPath = "E://";
        String targetFileName = "output.txt";

        try(InputStream inputStream = new FileInputStream(sourcePath + sourceileName);
            OutputStream outputStream = new FileOutputStream(targetPath + targetFileName)
        ){
            int BUFFER_SIZE = 1024;
            int read;
            byte[] bytes = new byte[BUFFER_SIZE];
            while((read = inputStream.read(bytes)) >= 0){
                outputStream.write(bytes);
                System.out.println(read);
            }
        }
    }




}













